import 'dart:convert';

enum QuestionResultType {
  Undefind,
  Right,
  Error,
  Null,
}

class Question {
  String quesId;
  int type;
  String title;
  String rightAnswer;
  String userAnswer;
  String analysis;
  int blankCount;
  List<AnswerOption> options;
  bool isCollect;
  bool isDeleted;

  // 填空题乱序
  bool answDisorder;
  // 答题流程中 - 是否确认答题的标记
  bool confirmed = false;

  bool get answered => !(userAnswer == null || userAnswer.isEmpty);

  Question({
    this.quesId,
    this.type,
    this.title,
    this.blankCount,
    this.rightAnswer,
    this.userAnswer,
    this.options,
    this.isCollect,
    this.analysis,
    this.confirmed,
    this.isDeleted,
    this.answDisorder,
  });

  Map<String, dynamic> toConstExamJson() {
    return {
      'id': this.quesId,
      'quesType': this.type,
      'info': this.title,
      'answ': this.rightAnswer,
      'selfAnsw': this.userAnswer,
      'analysis': this.analysis,
      'confirmed': this.confirmed,
      'answDisorder': this.answDisorder == true ? 1 : 0,
      'optionList': this.options.map((e) => e.toJson()).toList(),
    };
  }

  factory Question.fromConstExamJson(Map<String, dynamic> json) {
    int blankCount = 0;
    if (json['quesType'] == 4 /* 填空题 */ && json['answ'] != null) {
      String answ = json['answ'];
      if (answ.isNotEmpty) {
        blankCount = (jsonDecode(answ) as List).length;
      }
    }

    return Question(
      quesId: json['id'].toString(),
      type: json['quesType'],
      title: json['info'],
      rightAnswer: json['answ'] ?? '',
      userAnswer: json['selfAnsw'] ?? '',
      blankCount: blankCount,
      isCollect: false,
      analysis: json['analysis'] ?? '',
      confirmed: json['confirmed'] ?? false,
      isDeleted: false,
      answDisorder: json['answDisorder'] == 0 ? false : true,
      options: json['optionList'] == null
          ? []
          : (json['optionList'] as List).map((item) {
              return AnswerOption.fromJson(item);
            }).toList(),
    );
  }

  factory Question.fromJson(Map<String, dynamic> json) {
    return Question(
      quesId: json['quesId'],
      type: json['quesType'],
      title: json['info'],
      blankCount: json['blackCount'] ?? 0,
      rightAnswer: json['rightAnsw'] ?? '',
      userAnswer: json['selfAnsw'] ?? '',
      analysis: json['analysis'] ?? '',
      isCollect: json['collQues'] ?? false,
      confirmed: json['comfirmed'] ?? false,
      isDeleted: json['exists'] == false,
      answDisorder: json['answDisorder'] == 0 ? false : true,
      options: json['questionOptionList'] == null
          ? []
          : (json['questionOptionList'] as List).map((item) {
              return AnswerOption.fromJson(item);
            }).toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'quesId': this.quesId,
      'quesType': this.type,
      'info': this.title,
      'blankCount': this.blankCount,
      'rightAnsw': this.rightAnswer,
      'selfAnsw': this.userAnswer,
      'analysis': this.analysis,
      'collQues': this.isCollect,
      'confirmed': this.confirmed,
      'answDisorder': this.answDisorder == true ? 1 : 0,
      'questionOptionList': this.options.map((e) => e.toJson()).toList(),
    };
  }
}

class AnswerOption {
  String key;
  String option;
  bool checked;

  AnswerOption({this.key, this.option, this.checked});

  static AnswerOption fromJson(Map<String, dynamic> json) {
    return AnswerOption(
      key: json['key'],
      option: json['option'],
      checked: json['checked'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'key': this.key,
      'option': this.option,
      'checked': this.checked,
    };
  }
}
